import pygame, os

from bruce.config import Config
from bruce.page import Page
from bruce.fonts import get_font

font = get_font(Config.title_font, 48)

BORDER = 40

# TODO use PIL for scaling down - it's so much better at it

class ImagePage(Page):
    def __init__(self, title, image):
        self.title = title
        self.image = image

    def init(self, screen):
        # init title
        self.title = font.render(self.title, 1, Config.title_fgcolour)

        # load image and resize
        image = pygame.image.load(self.image).convert()
        w = screen.get_rect().width - BORDER*2
        h = screen.get_rect().height - BORDER*2
        imw, imh = image.get_rect().size
        if imw > w or imh > h:
            if imw > imh:
                ratio = float(w) / imw
                imw = w
                imh *= ratio
            else:
                ratio = float(h) / imh
                imh = h
                imw *= ratio
        self.image = pygame.transform.scale(image, (int(imw), int(imh)))

    def render(self, screen, deltat):
        screen.fill(Config.page_bgcolour)

        w = screen.get_rect().width
        tw = self.title.get_rect().width
        screen.blit(self.title, (w/2-tw/2, 0))

        # border
        w -= BORDER*2
        h = screen.get_rect().height

        imw, imh = self.image.get_rect().size
        screen.blit(self.image, (BORDER + w/2 - imw/2, BORDER + h/2 - imh/2))

    def imageHtml(self, out, exportDir, size=(320,240)):
        import Image
        import os
        imagesDir = os.path.join(exportDir,"images")
        try:
            os.makedirs(imagesDir)
        except OSError:
            pass
        image = Image.open(self.image)
        image.load()
        scaled=pygame.Rect(0,0,*size)
        destSize=pygame.Rect((0,0),image.size).fit(scaled).size
        image = image.resize(destSize, Image.BICUBIC)
        filename = os.path.basename(self.image)
        image.save(os.path.join(imagesDir, filename))
        url = "images/" + filename
        out.write("""<img src="%s" alt="(imagen)">\n"""%url)

    def renderHtml(self, out, exportDir):
        from cgi import escape
        out.write("<h1>%s</h1>\n"%escape(self.title))
        self.imageHtml(out, exportDir)
